﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;

namespace HaiFang
{
    /// <summary>
    /// Summary description for ModuleSubFieldInfoHandler
    /// </summary>
    public class ModuleSubFieldInfoHandler : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            if (!String.IsNullOrEmpty(context.Request.QueryString["action"]))
            {
                string action = context.Request.QueryString["action"];
                switch (action)
                {
                    case "GetModuleSubFieldInfo":
                        {
                            string Params = context.Request.QueryString["Params"];
                            ResponseGetModuleSubFieldInfo(context, Params);
                        }
                        break;
                    default:
                        WebHelper.WriteJsonError(context, "未知的操作.");
                        break;
                }
            }
            else if (context.Request.Form.Count > 0)
            {
                string action = context.Request.Form["action"];
                switch (action)
                {
                    case "AddModuleSubFieldInfo":
                        {
                            string Params = context.Request.Form["Params"];
                            if (!string.IsNullOrEmpty(Params))
                            {
                                ResponseAddModuleSubFieldInfo(context, Params);
                            }
                        }
                        break;
                    case "DelModuleSubFieldInfo":
                        {
                            string Params = context.Request.Form["Params"];
                            if (!string.IsNullOrEmpty(Params))
                            {
                                ResponseDelModuleSubFieldInfo(context, Params);
                            }
                        }
                        break;
                    case "SaveModuleSubFieldInfo":
                        {
                            string Params = context.Request.Form["Params"];
                            if (!string.IsNullOrEmpty(Params))
                            {
                                ResponseSaveModuleSubFieldInfo(context, Params);
                            }
                        }
                        break;
                    case "OrderModuleSubFieldInfo":
                        {
                            string Params = context.Request.Form["Params"];
                            if (!string.IsNullOrEmpty(Params))
                            {
                                ResponseOrderModuleSubFieldInfo(context, Params);
                            }
                        }
                        break;
                    default:
                        WebHelper.WriteJsonError(context, "未知的操作");
                        break;
                }
            }
        }

        private void ResponseGetModuleSubFieldInfo(HttpContext context, string Params)
        {
            try
            {
                IList<ModuleSubFieldInfo> list = ProxyModuleSubFieldInfo.QueryModuleSubFieldInfo(WebHelper.GetCurrentUserID(), Convert.ToInt32(Params), 0);
                WebHelper.WriteJsonResult(context, list);
            }
            catch (Exception ex)
            {
                LogHelper.Writelog(LogType.LogType_Error, ex.Message, ex);
                WebHelper.WriteJsonError(context, null);
            }
        }

        private void ResponseAddModuleSubFieldInfo(HttpContext context, string Params)
        {
            try
            {
                int ErrorMSG = -1;
                ModuleSubFieldInfo r = null;
                if (!String.IsNullOrEmpty(Params))
                {
                    r = JsonHelper.JsonDeserialize<ModuleSubFieldInfo>(Params);
                    if (r != null)
                    {
                        ProxyModuleSubFieldInfo.InsertModuleSubTable(WebHelper.GetCurrentUserID(), 1, 0,
                            r.ModuleSubID, r.FieldName, r.FieldType, r.FieldLen, r.QueryFlag, r.ResultFlag,
                            r.SpecialFlag, r.IniValue, r.BlankFlag, r.Description, out ErrorMSG);
                        WebHelper.WriteJsonResult(context, ErrorMSG);
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Writelog(LogType.LogType_Error, ex.Message, ex);
            }
            WebHelper.WriteJsonError(context, null);
        }

        private void ResponseDelModuleSubFieldInfo(HttpContext context, string Params)
        {
            try
            {
                int ErrorMSG = -1;
                ModuleSubFieldInfo r = null;
                if (!String.IsNullOrEmpty(Params))
                {
                    r = JsonHelper.JsonDeserialize<ModuleSubFieldInfo>(Params);
                    if (r != null)
                    {
                        ProxyModuleSubFieldInfo.InsertModuleSubTable(WebHelper.GetCurrentUserID(), 3, r.AutoID,
                            r.ModuleSubID, r.FieldName, r.FieldType, r.FieldLen, r.QueryFlag, r.ResultFlag,
                            r.SpecialFlag, r.IniValue, r.BlankFlag, r.Description, out ErrorMSG);
                        WebHelper.WriteJsonResult(context, ErrorMSG);
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Writelog(LogType.LogType_Error, ex.Message, ex);
            }
            WebHelper.WriteJsonError(context, null);
        }

        private void ResponseSaveModuleSubFieldInfo(HttpContext context, string Params)
        {
            try
            {
                int ErrorMSG = -1;
                ModuleSubFieldInfo r = null;
                if (!String.IsNullOrEmpty(Params))
                {
                    r = JsonHelper.JsonDeserialize<ModuleSubFieldInfo>(Params);
                    if (r != null)
                    {
                        ProxyModuleSubFieldInfo.InsertModuleSubTable(WebHelper.GetCurrentUserID(), 2, r.AutoID,
                            r.ModuleSubID, r.FieldName, r.FieldType, r.FieldLen, r.QueryFlag, r.ResultFlag,
                            r.SpecialFlag, r.IniValue, r.BlankFlag, r.Description, out ErrorMSG);
                        WebHelper.WriteJsonResult(context, ErrorMSG);
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Writelog(LogType.LogType_Error, ex.Message, ex);
            }
            WebHelper.WriteJsonError(context, null);
        }

        private void ResponseOrderModuleSubFieldInfo(HttpContext context, string Params)
        {
            try
            {
                int ErrorMSG = 0;
                OrderModuleSubFieldInfo r = null;
                if (!String.IsNullOrEmpty(Params))
                {
                    r = JsonHelper.JsonDeserialize<OrderModuleSubFieldInfo>(Params);
                    if (r != null)
                    {
                        switch (r.OrderType)
                        {
                            case (int)(SubFieldInfoOrderType.Top):
                                {
                                    for (int i = r.IndexInfo.DisplayID; i > 1; i--)
                                    {
                                        int UpAutoID = r.IndexInfo.AutoID;
                                        int DownAutoID = r.ModuleSubFieldInfo[i - 2].AutoID;
                                        ProxyModuleSubFieldInfo.OrderModuleSubFieldInfo(WebHelper.GetCurrentUserID(),
                                            r.IndexInfo.ModuleSubID, UpAutoID, DownAutoID);

                                    }
                                }
                                break;
                            case (int)(SubFieldInfoOrderType.Up):
                                {
                                    int UpAutoID = r.IndexInfo.AutoID;
                                    int DownAutoID = r.IndexInfo.AutoID;
                                    if (r.IndexInfo.DisplayID > 1)
                                    {
                                        DownAutoID = r.ModuleSubFieldInfo[r.IndexInfo.DisplayID - 2].AutoID;
                                    }
                                    ProxyModuleSubFieldInfo.OrderModuleSubFieldInfo(WebHelper.GetCurrentUserID(),
                                        r.IndexInfo.ModuleSubID, UpAutoID, DownAutoID);
                                }
                                break;
                            case (int)(SubFieldInfoOrderType.Down):
                                {
                                    int UpAutoID = r.IndexInfo.AutoID;
                                    int DownAutoID = r.IndexInfo.AutoID;
                                    if (r.IndexInfo.DisplayID < r.ModuleSubFieldInfo.Count)
                                    {
                                        UpAutoID = r.ModuleSubFieldInfo[r.IndexInfo.DisplayID].AutoID;
                                    }
                                    ProxyModuleSubFieldInfo.OrderModuleSubFieldInfo(WebHelper.GetCurrentUserID(),
                                        r.IndexInfo.ModuleSubID, UpAutoID, DownAutoID);
                                }
                                break;
                            case (int)(SubFieldInfoOrderType.Bottom):
                                {
                                    for (int i = r.IndexInfo.DisplayID; i < r.ModuleSubFieldInfo.Count; i++)
                                    {
                                        int DownAutoID = r.IndexInfo.AutoID;
                                        int UpAutoID = r.ModuleSubFieldInfo[i].AutoID;
                                        ProxyModuleSubFieldInfo.OrderModuleSubFieldInfo(WebHelper.GetCurrentUserID(),
                                            r.IndexInfo.ModuleSubID, UpAutoID, DownAutoID);

                                    }
                                }
                                break;
                            default:
                                {
                                    WebHelper.WriteJsonError(context, null);
                                    return;
                                }
                        }
                        WebHelper.WriteJsonResult(context, ErrorMSG);
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Writelog(LogType.LogType_Error, ex.Message, ex);
            }
            WebHelper.WriteJsonError(context, null);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}